home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSTHREAD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-26
|
5KB
|
162 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bsthread.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains for OS/2 threads.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
#define DEFAULT_STACK_SIZE (32 _K)
//----------------------------------------------------------------------------
// Description: Terminate thread. This function becomes a macro on
// operating systems without threads.
// Parameters: none
// Returns: void
//----------------------------------------------------------------------------
VOID FN_E __ThreadEnd(void)
{
Log(LOG_HIGH, "Thread %ld terminated.", (LONG)THREADID);
#if (OS_OS2 || OS_PM) && COMPILE_MT
_endthread();
#endif
return ;
}
//----------------------------------------------------------------------------
// Description: Get thread id.
// Parameters: none
// Returns: Thread ID. The ID of the main thread is always 1.
//----------------------------------------------------------------------------
TID FN_E ThreadId(void)
{
return THREADID;
}
//----------------------------------------------------------------------------
// Description: Start a thread
// Parameters: pfn Thread function
// ptid Pointer to variable to receive thread id.
// If null, thread id is not returned.
// pv Argument to thread function.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E ThreadStart(PFNTHRD pfn, PTID ptid, PVOID pv)
{
#if (OS_OS2 || OS_PM) && COMPILE_MT
int threadid = _beginthread(pfn, DEFAULT_STACK_SIZE, pv);
if (threadid <= 0)
{
Halt("Unable to start thread");
return FALSE;
}
Log(LOG_HIGH, "Thread %ld started", (LONG)THREADID);
if (ptid)
*ptid = (TID)threadid;
return TRUE;
#else
(*pfn)(pv); // Fake a thread!
if (ptid)
*ptid = (TID)1;
return TRUE;
#endif
}
//----------------------------------------------------------------------------
// Description: This is a thread function for the test function.
// Parameters: pv Pointer to string to print
//
// Returns:
//----------------------------------------------------------------------------
#if COMPILE_TEST
static BOOL fContinue = FALSE;
VOID FN_T TestThreadFunction(PVOID pv)
{
Output("Thread %d = %s\n", THREADID, pv);
fContinue = TRUE;
return ;
}
#endif
//----------------------------------------------------------------------------
// Description: Run standard test suite
// Parameters: sTest Test to run.
// 0 Run all default tests (except).
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_TEST
BOOL FN ThreadTest(SHORT sTest)
{
TID tid;
NOTUSED(sTest);
Output("Current thread id is %d\n", THREADID);
if (!ThreadStart(TestThreadFunction, &tid, "This is a test"))
return FALSE;
Output("Started thread %d\n", tid);
while (!fContinue) // While not done, give up time slice
Sleep(0);
return TRUE;
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------